package com.examsystem.config;

import com.examsystem.pojo.Paper;
import com.examsystem.pojo.vo.PaperClassVo;
import com.examsystem.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.Hashtable;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
 
/**
 * @author Administrator
 */
@Component
public class DynamicTaskService {
    private Hashtable<String, PaperClassVo> paperMap = new Hashtable();


    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    @Autowired
    private PaperService paperService;
 
 
    private ScheduledFuture<?> future;
 
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }
 
    public void addPaper(PaperClassVo  paper) {
        if (future == null) {
            //开启定时任务
            startCron();
        }
        System.out.println("添加交卷" + new Date().toLocaleString());
        paperMap.put(String.valueOf(paper.getPaper().getPaperId()), paper);
    }
 
    public String startCron() {
        future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("0/5 * * * * ? "));
        System.out.println("启动--定时轮询交卷任务");
        return "startCron";
    }
 
    public String stopCron() {
        if (future != null) {
            future.cancel(true);
            future = null;
            paperMap.clear();
        }
        System.out.println("停止--定时轮询交卷任务");
        return "stopCron";
    }

    public class MyRunnable implements Runnable {
        @Override
        public void run() {
            long timeCurrent = System.currentTimeMillis();
            Set keys = paperMap.keySet();
            for (Object key : keys) {
                PaperClassVo paper = paperMap.get(key);
                if (paper == null) {
                    paperMap.remove(key);
                    continue;
                }
                //计算结束时间(在原来基础上加5秒钟)
                //long sotpTime = paper.getBeginTime().getTime() + paper.getTime() * 60 * 1000l + 5000;
                long stopTime = (long) paper.getExamTime() *60*1000;
                if (stopTime <= timeCurrent) {
                    //时间结束调用交卷方法
                    try {
                        paperService.putInPaper(paper.getPaper().getPaperId(),1,paper.getPcClass());
                        System.out.println("执行交卷" + new Date().toLocaleString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //移除数据
                    paperMap.remove(key);
                }
            }
            if (paperMap.size() == 0) {
                //停止轮询
                stopCron();
            }
        }
    }
}